<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Library\Logic\OperateLogLogic;
use App\Library\Requests\Base\ActivityZoneSaveRequest;
use App\Models\ActivityZoneModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;

class ActivityZoneController extends AdminController
{
    protected $title = '活动专区';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new ActivityZoneModel);
        $grid->column('id', 'ID')->sortable();
        $grid->column('serial_number', '序列号');
        $grid->column('title', '活动名称');
        $grid->column('top_file_id', '顶部背景图')->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('goods_file_id', '商品背景图')->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('page_file_id', '页面背景图')->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('banner_file_id', 'banner图')->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('page_color', '页面背景色');
        $grid->column('tab_font_color', 'tab文字颜色');

        $grid->column('type', '类型')->display(function ($column) {
            if ($column == 'auction') {
                return '拍卖商品';
            } elseif ($column == 'plug') {
                return '捡漏商品';
            } elseif ($column == 'special') {
                return '专区商品';
            } else {
                return '普通商品';
            }
        });
        $grid->column('url', '跳转地址');

        /*$grid->column(random(), '小程序页面路径')->display(function () {
            return '/pagesA/specialColumn/activityPage?activityId=' . $this->id;
        });*/
        $grid->column('sort', '排序');
        $grid->column('is_show', '是否展示')->using(__('l.openArr'))->label(__('l.openArrLabel'));
        $grid->disableCreateButton(false);
        $grid->disableActions(false);
        $grid->actions(function (Actions $actions) {
            $actions->disableEdit(false);
            $actions->disableDelete(false);
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * edit
     * @param $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content): Content
    {
        $model = ActivityZoneModel::findOrFail($id);

        return $content->title($this->title())->description(trans('admin.edit'))
            ->body(view('activity.form', ['json' => $model->toJson()]));
    }

    /**
     * create
     * @param Content $content
     * @return Content
     */
    public function create(Content $content): Content
    {
        return $content->title($this->title())->description(trans('admin.create'))
            ->body(view('activity.form'));
    }

    /**
     * save
     * @param ActivityZoneSaveRequest $request
     * @return JsonResponse
     */
    public function save(ActivityZoneSaveRequest $request): JsonResponse
    {
        $data = array_merge(handleRequest($request), ['operateId' => Admin::user()->id]);
        if (empty($data['serialNumber'])) {
            OperateLogLogic::add(Admin::user()->user_id);
            return success(ActivityZoneModel::insertForModel($data)->id);
        }
        $model = ActivityZoneModel::findBySerialNumber($data['serialNumber'], 'id');
        if (is_null($model)) return fail(433);
        OperateLogLogic::add(Admin::user()->user_id);
        return success(ActivityZoneModel::updateForModel($data, $model)->id);
    }
}
